local extension = Package:new("xiang")
extension.extensionName = "Euphonium"
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
    ["xiang"] = "三香",
}
local xiangzhi = General:new(extension,"xiangzhi","th_k_chui",3,3,General.Female)
local wenfeng = fk.CreateTriggerSkill{
    name = "wenfeng",
    frequency = Skill.Compulsory,
    events = {fk.Damaged},
    anim_type = "defensive",
    can_trigger = function(self, event, target, player, data)
      if player:hasSkill(self) and target == player then
        if event == fk.Damaged then
          return player:getMark("wenfeng-turn") == 0
        end
      end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:recover{
            who = player,
            num = 1,
            skillName = self.name
        }
            room:setPlayerMark(player, "wenfeng-turn", 1)
      end
  }
  local wenfeng2 = fk.CreateTriggerSkill{
    name = "#wenfeng2",
    frequency = Skill.Compulsory,
    events = {fk.Damage,},
    anim_type = "support",
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(self)
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local targets = data.to
        if event == fk.Damage then
            targets:drawCards(1, self.name)
        end
    end
  }
  local wenfeng_maxcards = fk.CreateMaxCardsSkill{
    name = "#wenfeng_maxcards",
    correct_func = function(self, player)
      if player:hasSkill(wenfeng.name) then
        return player.hp
      end
    end
  }
  local wenfeng3 = fk.CreateTriggerSkill{
    name = "#wenfeng3",
    frequency = Skill.Compulsory,
    events = {fk.DamageInflicted},
    anim_type = "masochism",
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(self) and data.damage > 1
    end,
    on_use = function(self, event, target, player, data)
        player:drawCards(2, self.name)
    end
  }
    xiangzhi:addSkill(wenfeng)
    wenfeng:addRelatedSkill(wenfeng2)
    wenfeng:addRelatedSkill(wenfeng3)
    wenfeng:addRelatedSkill(wenfeng_maxcards)
    local baiyue_filter = fk.CreateActiveSkill{
        name = "#baiyue_filter",
        min_card_num = 2,
        max_card_num = 2,
        visible = false,
        card_filter = function(self, to_select, selected)
          return table.every(selected, function(id)
            return Fk:getCardById(to_select).color == Fk:getCardById(id).color
          end)
        end,
        target_filter = function (self, to_select, selected)
          return false
        end,
        can_use = Util.FalseFunc,
      }
      
      local baiyue = fk.CreateTriggerSkill{
        name = "baiyue",
        anim_type = "support",
        events = {fk.TurnStart},
        can_trigger = function(self, event, target, player, data)
          return player:hasSkill(self) and #player:getCardIds("he") >= 2
        end,
        on_cost = function(self, event, target, player, data)
          local room = player.room
          local prompt = "#baiyue-invoke::"..target.id
          local result, dat = room:askForUseActiveSkill(player, "#baiyue_filter", prompt, true)
          if result then
            self.cost_data = dat.cards
            room:throwCard(dat.cards, self.name, player, player)
            player:drawCards(1, self.name)
            return true
          end
          return false
        end,
        on_use = function(self, event, target, player, data)
          local room = player.room
          local choices = {"draw", "recover","Cancel"}
          local choice = room:askForChoice(player, choices, self.name)
          if choice ~= "Cancel" then
            if choice == "draw" then
              target:drawCards(2, self.name)
            elseif choice == "recover" then
              room:recover({
                who = target,
                num = 1,
                recoverBy = player,
                skillName = self.name,
              })
            end
          end
        end,
      }
      baiyue:addRelatedSkill(baiyue_filter)
      xiangzhi:addSkill(baiyue)
      local zhuiyue = fk.CreateTriggerSkill{
        name = "zhuiyue",
        anim_type = "support",
        frequency = Skill.Limited,
        events = {fk.EnterDying},
        can_trigger = function(self, event, target, player, data)
            return player:hasSkill(self) and target ~= player and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
        end,
        on_cost = function (self, event, target, player, data)
            return player.room:askForSkillInvoke(player, self.name, nil, "#zhuiyue"..target.id)
          end,
          on_use = function(self, event, target, player, data)
            local room = player.room
            room:recover({ who = target, num = player.hp, recoverBy = player, skillName = self.name })
            room:damage{
                to = player,
                damage = player.hp,
                skillName = self.name,
              }
          end,
    }
    xiangzhi:addSkill(zhuiyue)



    local qingxiang = General:new(extension,"qingxiang","th_k_chui",4,4,General.Female)
    local qienuo = fk.CreateTriggerSkill{
      name = "qienuo",
      events = {fk.EventPhaseStart},
      frequency = Skill.Compulsory,
      can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self) and player.phase == Player.Start
      end,
      on_use = function(self, event, target, player, data)
        local room = player.room
        player:broadcastSkillInvoke(self.name)
        room:damage{
          from = player,
          to = player,
          damage = 1,
          skillName = self.name,
        }
      end
    }
    qingxiang:addSkill(qienuo)
    local qienuoex = fk.CreateTriggerSkill{
      name = "qienuoex",
      events = {fk.EventPhaseStart},
      prompt = "#qienuoask",
      can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self) and player.phase == Player.Start
      end,
      on_use = function(self, event, target, player, data)
        local room = player.room
        player:broadcastSkillInvoke(self.name)
        room:damage{
          from = player,
          to = player,
          damage = 1,
          skillName = self.name,
        }
      end
    }
    qingxiang:addRelatedSkill(qienuoex)
    local xvwei = fk.CreateTriggerSkill{
      name = "xvwei",
      events = {fk.BeforeDrawCard},
      anim_type = "negative",
      frequency = Skill.Compulsory,
      can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(self) and player.phase == Player.Draw
      end,
      on_use = function(self, event, target, player, data)
        data.fromPlace = "bottom"
      end,
    }
    local xvwei_view = fk.CreateFilterSkill{
      name = "#xvwei_view",
      card_filter = function(self, to_select, player)
        --FIXME: filter skill isn't status skill, can't filter card which exists before hp change
        return player:hasSkill(self) and not player:getEquipment(Card.SubtypeArmor) and to_select.trueName == "slash" and
        table.contains(player.player_cards[Player.Hand], to_select.id)
      end,
      view_as = function(self, to_select)
        return Fk:cloneCard("jink", to_select.suit, to_select.number)
      end,
    }
    qingxiang:addSkill(xvwei)
    xvwei:addRelatedSkill(xvwei_view)
    local jianren_maxcards = fk.CreateMaxCardsSkill{
      name = "#jianren_maxcardss",
      fixed_func = function(self, player)
        if player:hasSkill(self) then
          return player.maxHp
        end
      end
    }
    local jianren = fk.CreateTriggerSkill{
      name = "jianren",
      anim_type = "masochism",
      events = {fk.Damaged},
      can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self) and not target.dead and target == player
      end,
      on_use = function(self, event, target, player, data)
        local room = player.room
        local from = data.from
        local cards = room:getNCards(3)
        local result = room:askForArrangeCards(player, self.name, {cards, "Top", from.general, player.general}, "#jianren",
    false, 0, {3, 1, 1}, {0, 1, 1})
        local top, bottom = result[2], result[3]
    local moveInfos = {}
    if #top > 0 then
      table.insert(moveInfos, {
        ids = top,
        to = from.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonGive,
        proposer = player.id,
        skillName = self.name,
        visiblePlayers = player.id,
      })
      for _, id in ipairs(top) do
        table.removeOne(cards, id)
      end
    end
    if #bottom > 0 then
      table.insert(moveInfos, {
        ids = bottom,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = self.name,
      })
      for _, id in ipairs(bottom) do
        table.removeOne(cards, id)
      end
    end
    if #cards > 0 then
      for i = #cards, 1, -1 do
        table.insert(room.draw_pile, 1, cards[i])
      end
    end
    room:moveCards(table.unpack(moveInfos))
  end,
    }
    qingxiang:addSkill(jianren)
    jianren:addRelatedSkill(jianren_maxcards)
    local qx_jishi = fk.CreateTriggerSkill{
      name = "qx_jishi",
      anim_type = "defensive",
      events = {fk.EnterDying},
      frequency = Skill.Wake,
      can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(self) and
          player:usedSkillTimes(self.name, Player.HistoryGame) == 0
      end,
      can_wake = function(self, event, target, player, data)
        return player.dying
      end,
      on_use = function(self, event, target, player, data)
        local room = player.room
        room:changeMaxHp(player, -1)
        room:recover({
          who = player,
          num = 2 - player.hp,
          recoverBy = player,
          skillName = self.name
        })
        room:handleAddLoseSkills(player, "qx_juexiang", nil, true, false)
        room:handleAddLoseSkills(player, "-xvwei", nil, true, false)
    if player:hasSkill("qienuo", true) then
      room:handleAddLoseSkills(player, "-qienuo", nil, true, false)
      room:handleAddLoseSkills(player, "qienuoex", nil, true, false)
    end
  end,
}
qingxiang:addSkill(qx_jishi)
local qx_juexiang = fk.CreateActiveSkill{
  name = "qx_juexiang",
  anim_type = "drawcard",
  min_card_num = 1,
  max_card_num = 4,
  card_filter = function(self, to_select, selected)
    local card = Fk:getCardById(to_select)
    return not Self:prohibitDiscard(card) and card.suit ~= Card.NoSuit and
    table.every(selected, function (id) return card.suit ~= Fk:getCardById(id).suit end)
  end,
  target_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local targets = table.map(room.alive_players, Util.IdMapper)
    local target = room:askForChoosePlayers(player, targets, 1, 1, "#qx_juexiang_slash-ask")
    local tos = target[1]
    local to = room:getPlayerById(tos)
    local from = room:getPlayerById(effect.from)
    local hand = from:getCardIds(Player.Hand)
    local more = #hand > 0
    for _, id in ipairs(hand) do
      if not table.contains(effect.cards, id) then
        more = false
        break
      end
    end
    room:throwCard(effect.cards, self.name, from, from)
    room:drawCards(from, #effect.cards + (more and 1 or 0), self.name)
    local cardnum = #effect.cards
    if cardnum == 4 then
      room:damage{
        from = player,
        to = to,
        damage = 1,
        skillName = self.name,
      }
    end
  end
}
qingxiang:addRelatedSkill(qx_juexiang)

local tianzhongmingrixiang = General:new(extension,"tianzhongmingrixiang","th_k_chui",4,4,General.Female)
local mingjing = fk.CreateTriggerSkill{
  name = "mingjing",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room 
    local result = room:askForGuanxing(player, room:getNCards(3))
   if #result.top == 0 then
    local targets = table.map(table.filter(room.alive_players, function(p) return not p:isAllNude() end), Util.IdMapper)
    local to = room:askForChoosePlayers(player,targets, 1, 1, "#mingjingchosse", self.name, true)
    if #to>0 then
    local p = room:getPlayerById(to[1])
    U.viewCards(player, p:getCardIds(Player.Hand), self.name, "#mingjing::"..target.id)
  end
end
  end,
}
tianzhongmingrixiang:addSkill(mingjing)
local mrx_zhenwei = fk.CreateTriggerSkill{
  name = "mrx_zhenwei",
  anim_type = "defensive",
  events = {fk.TargetConfirming},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.card.is_damage_card then
    local card_trueName = data.card.trueName
    local mark = player:getMark("mrxzhenwei-turn")
    return   (type(mark) ~= "table" or not table.contains(mark, card_trueName))
  end
end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = room:getPlayerById(data.from)
    local judge = {
      card = Fk:getCardById(room:getNCards(1,"bottom")[1]),
      who = player,
      reason = self.name,
      pattern = ".|.|spade,club,heart,diamond",
    }
    room:judge(judge)
    if judge.card.color == Card.Red then
        player:drawCards(1,self.name)
    else
        local id = room:askForCardChosen(player, from, "h", self.name)
        room:throwCard(id,self.name,from,player)
      end
      local mark = player:getMark("mrxzhenwei-turn")
      if type(mark) ~= "table" then mark = {} end
      local card_trueName = data.card.trueName
      table.insert(mark, card_trueName)
      room:setPlayerMark(player,"mrxzhenwei-turn",mark)
  end,
}

tianzhongmingrixiang:addSkill(mrx_zhenwei)
local caibei = fk.CreateActiveSkill{
  name = "caibei",
  anim_type = "drawcard",
  target_num = 0,
  can_use = function(self, player)
    return (player:getMark("caibei_basic-phase") == 0 or player:getMark("caibei_trick-phase") == 0)
  end,
  interaction = function()
    local choices = {}
    local handcards = Self:getCardIds(Player.Hand)
    local trick = table.filter(handcards, function (id)
      return Fk:getCardById(id).type ~= Card.TypeBasic
    end)
    if Self:getMark("caibei_basic-phase") == 0 then table.insert(choices, "caibei_basic") end
    if Self:getMark("caibei_trick-phase") == 0 and #trick~=0 then table.insert(choices, "caibei_trick") end
    return UI.ComboBox {choices = choices }
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:setPlayerMark(player, self.interaction.data.."-phase", 1)
    if player.dead then return end
    if self.interaction.data == "caibei_basic" then
      player:drawCards(2)
      local hand = player:getCardIds(Player.Hand)
      local cards = table.filter(player:getCardIds(Player.Hand), function (id)
        local card = Fk:getCardById(id)
        return card.type == Card.TypeBasic and not player:prohibitDiscard(card)
      end)
      if #cards > 0 then
        room:throwCard(cards, self.name, player)
        local more = #hand > 0
        for _, id in ipairs(hand) do
          if not table.contains(cards, id) then
           more = false break
        end
      end
        room:drawCards(player, (more and 1 or 0), self.name)
      end
    else
      local cards = table.filter(player:getCardIds(Player.Hand), function (id)
        local card = Fk:getCardById(id)
        return card.type ~= Card.TypeBasic and not player:prohibitDiscard(card)
      end)
      if #cards > 0 then
        local hand = player:getCardIds(Player.Hand)
        room:throwCard(cards, self.name, player)
        room:addPlayerMark(player, MarkEnum.SlashResidue.."-phase", player.hp)
        local more = #hand > 0
        for _, id in ipairs(hand) do
          if not table.contains(cards, id) then
           more = false break
        end
      end
        room:drawCards(player, (more and 1 or 0), self.name)
        player:drawCards(2)
    end
  end
  end
}
local caibei_trigger = fk.CreateTriggerSkill{
  name = "#caibei_trigger",
  anim_type = "drawcard",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryPhase) <player.hp and player:getMark("caibei_basic-phase") ~= 0 then
      return data.card.type == Card.TypeTrick
    end 
  end,
  on_use = function(self, event, target, player, data)
      player:drawCards(1, self.name)
  end,
}
tianzhongmingrixiang:addSkill(caibei)
caibei:addRelatedSkill(caibei_trigger)

    Fk:loadTranslationTable{
        ["xiangzhi"] = "中世古香织",
        ["#xiangzhi "] = "吹奏部天使",
        ["wenfeng"] = "温风",
        ["#wenfeng2"] = "温风",
        ["#wenfeng3"] = "温风：摸牌",
        [":wenfeng"] = "锁定技:你的手牌上限+X。你每回合首次受到伤害后你回复一点体力，当你受到不小于2的伤害时,你摸两张牌;当你造成伤害时,受到伤害的角色摸一张牌。",
        ["baiyue"] = "白月",
        ["#baiyue_filter"] = "白月",
        [":baiyue"] = "一名角色回合开始时，你可以弃置两张颜色相同的牌并摸一张牌,然后选择一项:1.令其摸两张牌,2.令其回复1点体力。",
        ["draw"] = "令其摸两张牌",
        ["recover"] = "令其回复1点体力。",
        ["#baiyue-invoke"] = "是否发动白月",
        ["zhuiyue"] = "坠月",
        [":zhuiyue"] = "限定技：当一名角色进入濒死时,你可以对自己造成X点伤害，然后该角色回复X点体力。（X为你的体力值)",
        ["#zhuiyue"] = "是否发动坠月",
        ["qingxiang"] = "小笠原晴香",
        ["#qingxiang"] = "温柔的部长",
        ["qienuo"] = "怯懦",
        [":qienuo"]= "锁定技：准备阶段，你对自己造成一点伤害。",
        ["qienuoex"] = "怯懦",
        [":qienuoex"] = "准备阶段，你可以对自己造成一点伤害。",
        ["#qienuoask"] = "是否发动怯懦对自己造成一点伤害",
        ["xvwei"] = "虚位",
        [":xvwei"] = "摸牌阶段，你改为从排堆底摸牌。当你没有装备武器牌时，你的【杀】均视为【闪】",
        ["#xvwei_view"] = "虚位",
        ["jianren"] = "坚韧",
        [":jianren"] = "你的手牌上限等于体力上限；你受到伤害时,你可以观看牌堆顶三张牌，然后交于你与伤害来源各一张。",
        ["#jianren"] = "请分配卡牌，余下的牌置于牌堆顶",
        ["qx_jishi"] = "积势",
        [":qx_jishi"] = "觉醒技：当你进入濒死时，你减少一点体力上限，将体力回复至2点，修改怯懦为非锁定技，然后失去“虚位”，获得“绝响”。",
        ["qx_juexiang"] = "绝响",
        [":qx_juexiang"] = "出牌阶段限一次,你可以弃置任意张花色各不相同的牌,然后摸等量的牌，若你弃置了所有手牌，则你多摸一张牌,若你弃置的花色为4种，则你可以对一名角色造成1点伤害。",
        ["#qx_juexiang_slash-ask"] = "若你弃置了四种不同花色的牌，选择一名角色对其造成一点伤害",
        ["tianzhongmingrixiang"] = "田中明日香",
        ["#tianzhongmingrixiang"] = "北宇治之母",
        ["mingjing"] = "明镜",
        [":mingjing"] = "准备阶段,你可以观看牌堆底三张牌然后按照任意顺序置于牌堆顶或牌堆底,若你将这些牌均放至牌堆底,则你可以观看一名角色的手牌。",
        ["#mingjingchosse"] = "选择一名角色观看其手牌",
        ["#mingjing"] = "明镜",
        ["mrx_zhenwei"] = "真伪",
        [":mrx_zhenwei"] = "当你每回合首次成为一种伤害类牌的目标时,你可以从牌堆底进行判定,若为红,你摸一张牌，为黑,你弃置该角色一张手牌。",
        ["caibei"] = "才备",
        [":caibei"] = "出牌阶段各限一次,你可以:1.弃置所有非基本牌并摸两张牌,然后本回合使用杀次数+X,2.摸两张牌并弃置所有基本牌，然后本回合前X次使用锦囊牌时,你摸一张牌。若你弃置了所有手牌，你摸一张牌。（×为你的体力值,弃置的牌数量至少为1)",
        ["caibei_basic"] = "弃置所有基本牌",
        ["caibei_trick"] = "弃置所有非基本牌",
        ["#caibei_trigger"] = "才备",
        ["mrx_qiyuan"] = "期愿"
    }
return extension